﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Data;
using BLL;

public partial class Spa_xiugai : WeiPage
{
    PersonInfoBO pbo = new PersonInfoBO();
    IntegralChangeBO ibo = new IntegralChangeBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                PersonInfo per = pbo.GetModel(user);
                           
                    DataSet city = pbo.GetCityCode();

                    foreach (DataRow dr in city.Tables[0].Rows)
                    {
                        qy.Items.Add(new ListItem(dr["name"].ToString(), dr["code"].ToString()));
                    }

                    if (per != null)
                    {
                        qy.SelectedValue = per.area;
                        xm.Value = per.Name;
                        sr.Value = DateTime.Parse(per.BIRTHDAY.ToString()).ToString("yyyy-MM-dd");
                        txtPhone.Value = per.Phone;
                        ddlSex.SelectedValue = per.Sex == true ? "2" : "1";
                        dz.Value = per.addr;
                        bz.Value = per.REMARK;
                    }
                    else
                    {
                        Response.Redirect("weixinreg2.aspx");
                    }
                
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                PersonInfo per = pbo.GetModel(user);
                if (per != null)
                {
                    if (qy.SelectedValue == "0")
                    {
                        Label1.Text = "请选择区域";
                    }
                    else if (xm.Value == "")
                    {
                        Label1.Text = "请输入姓名";
                    }
                    else if (ddlSex.SelectedValue == "-1")
                    {
                        Label1.Text = "请选择性别";
                    }
                    else
                    {
                        per.area = qy.SelectedValue;
                        per.Name = xm.Value;
                        per.Sex = ddlSex.SelectedValue == "1" ? true : false;
                        per.addr = dz.Value;
                        per.REMARK = bz.Value;
                        int id = pbo.Update(per);
                        if (id > 0)
                        {
                            si_DispMember_obService s1 = new si_DispMember_obService();
                            dt_DispMember_req w1 = new dt_DispMember_req();
                            w1.ZTYPE = "0101";
                            w1.MOB_NUMBER = txtPhone.Value.Trim();
                            dt_DispMember_res dt1 = s1.si_DispMember_ob(w1);
                            if (dt1.ZCRMT316 != null)
                            {
                                si_UploadMemberShip_obService s = new si_UploadMemberShip_obService();
                                dt_UploadMemberShip_req w = new dt_UploadMemberShip_req();
                                ZCRMT316 z = new ZCRMT316();
                                z.TYPE = "0101";
                                z.NAME1_TEXT = per.Name;
                                z.BPEXT = dt1.ZCRMT316[0].BPEXT;
                                z.MOB_NUMBER = per.Phone;
                                z.XSEX = per.Sex ? "2" : "1";
                                z.PSTREET = per.addr;
                                w.ZCRMT316 = new ZCRMT316[] { z };
                                dt_UploadMemberShip_res dt = s.si_UploadMemberShip_ob(w);
                                if (dt != null && dt.WV_RETURN == "Y")
                                {
                                    Label1.Text = "修改成功";
                                }
                                else
                                {
                                    Label1.Text = "修改失败";
                                }
                            }
                            else
                            {
                                Label1.Text = "未获取到用户信息";
                            }

                        }

                    }
                }
                else
                {
                    Label1.Text = "您尚未注册，请先注册";
                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message.ToString();
        }
    }
}